import axios from 'axios'
import Vue from 'vue'
axios.defaults.timeout = 100000
// axios.defaults.headers['Content-Type'] = 'application/json'
// axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded'
// axios.defaults.headers['Content-Type'] = 'multipart/form-data'
if (process.env.NODE_ENV === 'development') { // 开发环境
    axios.defaults.baseURL = ''
} else { // 生产环境
    axios.defaults.baseURL = 'https://zhaq-sit-manager.bosafe.com'
}
axios.interceptors.request.use(
    config => {
        return config
    },
    error => {
        return error
    })
axios.interceptors.response.use(
    res => {
        if (res.data.code !== 0 && res.data.code !== 6 && res.data.code !== 3 && res.data.code != 1 && res.data.info) {
            Vue.$vux.toast.text(res.data.info)
        }
        Vue.$vux.loading.hide()
        return res
    },
    error => {
        Vue.$vux.toast.text(error)
        Vue.$vux.loading.hide()
        return error
    })
export default {
    fetchGet (url, params = {}) {
        return axios.get(url, params).then((res) => {
            return Promise.resolve(res)
        }).catch((error) => {
            return Promise.reject(error)
        })
    },
    fetchPost (url, params = {}, formData = new FormData()) {
        formData.append('data', JSON.stringify(params))
        return axios({
            url: url,
            method: 'POST',
            data: formData
        }).then((res) => {
            return Promise.resolve(res)
        }).catch((error) => {
            return Promise.reject(error)
        })
    },
    fetchAsync (url = '', data = {}, type = 'GET', method = 'fetch') {
        if (type === 'GET') {
            return new Promise((resolve, reject) => {
                // get方法加个时间参数，解决ie可能缓存问题
                data.t = new Date().getTime()
                axios.get(url, {
                    params: data
                })
                    .then(response => {
                        resolve(response.data)
                    })
                    .catch(err => {
                        reject(err)
                    })
            })
        }
        if (type === 'POST') {
            return new Promise((resolve, reject) => {
                data.business = url
                if (localStorage.userInfo) {
                    this.userInfo = JSON.parse(localStorage.userInfo)
                    data.tokenId = this.userInfo.tokenId
                    data.userId = this.userInfo.userid
                } else {
                    data.tokenId = 'A3944C2D655B11D684296E71CB81C9BD'
                    data.userId = '615123137'
                }
                let formData = new FormData()
                formData.append('data', JSON.stringify(data))
                axios.post('/nzcgjsms/api/AppService.ashx', formData)
                // axios.post('/TestNzcgjsms/api/AppService.ashx', formData)
                    .then(response => {
                        resolve(response.data)
                    })
                    .catch(err => {
                        reject(err)
                    })
            })
        }
    }
}
